/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "lua_camera.h"

#include "../kernel.h"
#include "../render/icamera.h"
#include "../shared/vector.h"

extern Kernel* g_kernel;

/** Move the Camera to a location */
int LUA_Camera::MoveTo( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    float x = float(luaL_checknumber( L , 1 ));
    float y = float(luaL_checknumber( L , 2 ));

    cam->MoveTo( Vector2f(x,y) );
    return 0;
}

/** Add a vector to the camera path */
int LUA_Camera::AddToPath( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    float x = float(luaL_checknumber( L , 1 ));
    float y = float(luaL_checknumber( L , 2 ));

    cam->AddToPath( Vector2f(x, y) );
    return 0;
}

int LUA_Camera::Attach( lua_State* L )
{
    int camNum = int(luaL_checknumber( L, 1 ));
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( camNum );
    std::string name = luaL_checkstring( L , 2 );
    IEntity* ent = EntityManager::GetInstance()->Find( name );

    cam->Attach( ent );
    return 0;
}

int LUA_Camera::Activate( lua_State* L )
{
    int camNum = int(luaL_checknumber( L, 1 ));
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( camNum );
    int activate = luaL_checknumber( L , 2 );
    if ( cam )
    {
        cam->Activate( activate );
    }
    return 0;
}

/** Get the X coord of the camera */
int LUA_Camera::GetX(lua_State *L)
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    lua_pushnumber( L, cam->GetPosition().x );
    return 1;
}

/** Get the Y coord of the camera */
int LUA_Camera::GetY(lua_State *L)
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    lua_pushnumber( L, cam->GetPosition().y );
    return 1;
}

int LUA_Camera::Shake( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    float time = float(luaL_checknumber( L , 1 ));
    float svel = float(luaL_checknumber( L, 2 ));
    cam->Shake( time, svel );
    return 0;
}

int LUA_Camera::GetVelocity( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    lua_pushnumber( L, cam->GetVelocity().x );
    lua_pushnumber( L, cam->GetVelocity().y );

    return 2;
}
int LUA_Camera::SetVelocity( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    float x = float(luaL_checknumber( L , 1 ));
    float y = float(luaL_checknumber( L, 2 ));
    cam->SetVelocity( Vector2f( x, y ) );
    return 0;
}
int LUA_Camera::GetFriction( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    lua_pushnumber( L, cam->GetFriction() );
    return 1;
}

int LUA_Camera::SetFriction( lua_State* L )
{
    ICamera* cam = g_kernel->GetRenderer()->GetCamera( 0 );
    float f = float(luaL_checknumber( L , 1 ));
    cam->SetFriction( f );

    return 0;
}

// -- Bind to LUA
#define method(name) {#name, LUA_Camera::name}

const luaL_reg LUA_Camera::methods[] = {
        method(MoveTo),
        method(Activate),
        method(AddToPath),
        method(Attach),
        method(GetX),
        method(GetY),
        method(Shake),
        method(SetVelocity),
        method(GetVelocity),
        method(SetFriction),
        method(GetFriction),
        {NULL, NULL}
};

// - Bind to lua
int luaopen_LUA_Camera(lua_State *L) {
    luaL_openlib(L, "camera", LUA_Camera::methods, 0);
 	return 0;
}